package com.zy.qudadid.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zy.qudadid.model.Bill;
import com.zy.qudadid.model.Res;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.network.Net;
import com.zy.qudadid.presenter.base.RefreshAndLoadMorePresenter;
import com.zy.qudadid.ui.view.IndexView;

import org.apache.http.Header;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ninos on 2016/7/22.
 */
public class IndexPresenter extends RefreshAndLoadMorePresenter<IndexView> {

    private static AsyncHttpClient client = new AsyncHttpClient();

    @Override
    public void getData(int page, int count) {

    }

    //    http://112.74.98.38/%7B$api_url%7D/api.php?c=infoOrderCar&a=listenOrder&args=
//    public void listenOrder(String data) {
//        Subscription subscription = Net.getService()
//                .listenOrder("infoOrderCar", "listenOrder", data)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .onErrorReturn(new Func1<Throwable, Res<ArrayList<Bill>>>() {
//                    @Override
//                    public Res<ArrayList<Bill>> call(Throwable throwable) {
//
//                        Res res = new Res();
//                        res.code = 401;
//                        res.message = "失败";
//                        res.datas = new ArrayList<Bill>();
//
//                        return res;
//                    }
//                })
//                .subscribe(new Subscriber<Res<ArrayList<Bill>>>() {
//
//                    @Override
//                    public void onCompleted() {
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        try {
//                            view.errorB();
//                        } catch (Exception ex) {
//                        }
//                    }
//
//                    @Override
//                    public void onNext(Res<ArrayList<Bill>> res) {
//                        try {
//                            if (res.code == Const.OK) {
//                                view.success(res.datas);
//                            } else {
//                                view.errorB();
//                            }
//                        } catch (Exception ex) {
//                        }
//                    }
//                });
//
//        addSubscription(subscription);
//    }

    public void listenOrder(String data) {
        try{
            RequestParams params = new RequestParams();
            params.put("args", data);

            client.post("http://139.196.5.108/api.php?c=infoOrderCar&a=listenOrder", params,
                    new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(int i, Header[] headers, byte[] bytes) {
                            try {
                                if (i == 200) {
                                    try {
                                        String responseString = new String(bytes, HTTP.UTF_8);
                                        JSONObject jsonObject = new JSONObject(responseString);
                                        if (jsonObject.getString("code").equals("200")) {
//                                            Money money = new Gson().fromJson(jsonObject.getString("datas"), Money.class);
                                            ArrayList<Bill> list = new Gson().fromJson(jsonObject.getString("datas"), new TypeToken<ArrayList<Bill>>(){}.getType());
                                            view.success(list);
                                        }else{
//                                            view.error();
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }else
                                    view.error();
                            } catch (Exception e) {
                                view.error();
                            }
                        }

                        @Override
                        public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                            try {
                                view.error();
                            } catch (Exception e) {
                                view.error();
                            }
                        }
                    });
        }catch (Exception ex){}
    }

    //    api.php?c=infoOrderCar&a=acceptOrder&args=
    public void acceptOrder(String data, final Bill bill) {
        Subscription subscription = Net.getService()
                .acceptOrder("infoOrderCar", "acceptOrder", data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.error();
                    }

                    @Override
                    public void onNext(Res res) {
                        if (res.code == Const.OK) {
                            view.successBill(bill);
                        } else {
                            view.error();
                        }
                    }
                });

        addSubscription(subscription);
    }

}
